﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Main_Inventory_BOM : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string sql = "";
    int ControlCounter = 5;
    List<string> LstItemName;
    List<string> LstUnit;
    List<string> LstQty;

    protected void Page_Load(object sender, EventArgs e)
    {
       if (Request.QueryString["msg"] != null && Request.QueryString["msg"].ToString()=="insert")
       {
           lblMsg.Attributes.Remove("class");
           lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Bill Of Material created successfully...";
       }
        if (!IsPostBack)
        {
            HidControl.Value = ControlCounter.ToString();
            BindDDLItem();
            //Bind_Control();
          
            LstQty = new List<string>();
            ViewState["LstQty"] = LstQty;

            LstItemName = new List<string>();
            ViewState["LstItemName"] = LstItemName;

            LstUnit = new List<string>();
            ViewState["LstUnit"] = LstUnit;
        }
    }
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        LstItemName = (List<string>)ViewState["LstItemName"];
        LstQty = (List<string>)ViewState["LstQty"];
        LstUnit = (List<string>)ViewState["LstUnit"];
       for (int i = 0; i < LstItemName.Count; i++)
            {
                ControlCounter++;

                HtmlTableRow htrow = new HtmlTableRow();
                htrow.ID = "htrow" + ControlCounter.ToString();

                HtmlTableCell htcell1 = new HtmlTableCell();
                htcell1.ID = "htcell1" + ControlCounter.ToString();
                htcell1.Width = "19%";
                htcell1.VAlign = "Top";
                DropDownList dropdown = new DropDownList();
                dropdown.ID = LstItemName[i].ToString();
                dropdown.Width = Unit.Percentage(99);
             
                dropdown.AutoPostBack = true;
                dropdown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
                dropdown.CssClass = "dropdown";
                htcell1.Controls.Add(dropdown);


                HtmlTableCell htcell2 = new HtmlTableCell();
                htcell2.ID = "htcell2" + ControlCounter.ToString();
                htcell2.Width = "8%";
                htcell2.VAlign = "Top";
                TextBox TxtQty = new TextBox();
                TxtQty.ID = LstQty[i].ToString();
                TxtQty.Width = Unit.Percentage(99);
                TxtQty.Text = "0.00";
                //TxtQty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
                //TxtQty.Attributes.Add("onblur", "calcQty(); calcAmt();");
                //TxtQty.AutoCompleteType = AutoCompleteType.Disabled;
                TxtQty.CssClass = "inputnumber";
               


                HtmlTableCell htcell3 = new HtmlTableCell();
                htcell3.ID = "htcell3" + ControlCounter.ToString();
                htcell3.Width = "14%";
                htcell3.VAlign = "Top";
                TextBox TxtUnit = new TextBox();
                TxtUnit.ID = LstUnit[i].ToString();
                TxtUnit.Width = Unit.Percentage(99);
                TxtUnit.ReadOnly = true;
                TxtUnit.CssClass = "input";
                //TxtUnit.Attributes.Add("onKeyPress", "return false");
                //TxtUnit.AutoCompleteType = AutoCompleteType.Disabled;
                
                htcell3.Controls.Add(TxtQty); 
                htcell2.Controls.Add(TxtUnit);

                htrow.Cells.Add(htcell1);
                htrow.Cells.Add(htcell2);
                htrow.Cells.Add(htcell3);
                tblItem.Rows.Add(htrow);
                
            }
        }
    protected void BindDDLItem()
    {
        sql = "select ITMDESC, ITMID from IVR_ITEM where ITMID not in (Select Itmid from Ivr_BOM) order by ITMDESC ";
        DataTable dt = DU.getdata(sql);

        DDLItem.DataSource = dt;
        DDLItem.DataTextField = "ITMDESC";
        DDLItem.DataValueField = "ITMID";
        DDLItem.DataBind();

        DDLItem.Items.Insert(0, new ListItem("-Select Item-", "0"));
    }

    protected void Bind_Control(bool reload)
    {
        int dlcount = 0;
        int count = Convert.ToInt32(HidControl.Value);
        if (ViewState["prevcount"] == null)
        {
            ViewState["prevcount"] = 1;
        }
        else
        {
            ViewState["prevcount"] = count;
        }
        if (reload == true)
        {
            dlcount = 1;
        }
        else
        {

            dlcount = Convert.ToInt32(ViewState["prevcount"]);
        }
        for (int i = dlcount; i <= count; i++)
        {
            DropDownList DDL = (DropDownList)tblItem.FindControl("DDLItemName" + i.ToString());
            sql = "select ITMDESC, ITMID from IVR_ITEM where itmid!="+ DDLItem.SelectedValue+" order by ITMDESC";
            DataTable dt = DU.getdata(sql);
            DDL.DataSource = dt;
            DDL.DataTextField = "ITMDESC";
            DDL.DataValueField = "ITMID";
            DDL.DataBind();
            DDL.Items.Insert(0, new ListItem("-Select Item-", "0"));
        }
    }
    protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        DropDownList DDL = (DropDownList)sender;
       
        string ddid = DDL.ID.ToString().Substring(11);
       

        TextBox txtUnit = (TextBox)tblItem.FindControl("TxtUnit" + ddid);
        TextBox txtQty = (TextBox)tblItem.FindControl("TxtQty" + ddid);

         if (DDL.SelectedValue != "0")
         {
             string item_id = DDL.SelectedValue;
             string sql = "select UOMSYMBOL from IVR_UOM inner join IVR_ITEM on IVR_UOM.UOMID=IVR_ITEM.PRIMARY_UOMID where IVR_ITEM.                     ITMID='" + item_id + "'";
             DataTable DT = DU.getdata(sql);
             txtUnit.Text = DT.Rows[0][0].ToString();
           
         }
         else
         {
             
             txtUnit.Text = "";
         }
       
    }
    protected void btnAddMore_Click(object sender, EventArgs e)
    {

       ControlCounter++;
           
        HtmlTableRow htrow = new HtmlTableRow();
        //htrow.ID = "htrow" + ControlCounter.ToString();

        HtmlTableCell htcell1 = new HtmlTableCell();
        
       
        htcell1.VAlign = "Top";
        DropDownList dropdown = new DropDownList();
        dropdown.ID = "DDLItemName" + ControlCounter.ToString();
        dropdown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
        dropdown.Attributes.Add("onchange", "return checkItemRepetion(this.id)");
        //dropdown.Width = Unit.Percentage(99);
           // BindAllItemDropdownlist(dropdown);
        dropdown.AutoPostBack = true;
           // dropdown.SelectedIndexChanged += new EventHandler(ddlItem_SelectedIndexChanged);
        dropdown.CssClass = "dropdown";
        dropdown.Width = Unit.Percentage(99);
        htcell1.Controls.Add(dropdown);
        LstItemName.Add(dropdown.ID);

            HtmlTableCell htcell2 = new HtmlTableCell();
           
           
            htcell2.VAlign = "Top";
            TextBox TxtQty = new TextBox();
            TxtQty.ID = "TxtQty" + ControlCounter.ToString();
           // txtQty.Width = Unit.Percentage(99);
            TxtQty.CssClass = "inputnumber";
           // txtQty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
           // txtQty.Attributes.Add("onblur", "calcQty(); calcAmt();");
           // txtQty.AutoCompleteType = AutoCompleteType.Disabled;
            TxtQty.Width = Unit.Percentage(99);
            TxtQty.Text = "0.00";
            LstQty.Add(TxtQty.ID);

            HtmlTableCell htcell3 = new HtmlTableCell();
            
            htcell3.Width = "14%";
            htcell3.VAlign = "Top";
            TextBox TxtUnit = new TextBox();
            TxtUnit.ID = "TxtUnit" + ControlCounter.ToString();
            TxtUnit.Width = Unit.Percentage(99);
            TxtUnit.ReadOnly = true;
           // txtUnit.Width = Unit.Percentage(99);
            TxtUnit.CssClass = "input";
           // txtUnit.Attributes.Add("onKeyPress", "return false");
           // txtUnit.AutoCompleteType = AutoCompleteType.Disabled;

            htcell2.Controls.Add(TxtUnit);
            htcell3.Controls.Add(TxtQty);
            LstUnit.Add(TxtUnit.ID);

        

            htrow.Cells.Add(htcell1);
            htrow.Cells.Add(htcell2);
            htrow.Cells.Add(htcell3);
            tblItem.Rows.Add(htrow);


            ViewState["LstItemName"] = LstItemName;
            ViewState["LstQty"] = LstQty;
            ViewState["LstUnit"] = LstUnit;

            HidControl.Value = ControlCounter.ToString();
            Bind_Control(false);

    }
    protected void DDLItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLItem.SelectedValue != "0")
        {

            string item_id = DDLItem.SelectedValue;
            string sql = "select UOMSYMBOL from IVR_UOM inner join IVR_ITEM on IVR_UOM.UOMID=IVR_ITEM.PRIMARY_UOMID where IVR_ITEM.ITMID='" + item_id + "'";
            DataTable DT = DU.getdata(sql);
            TxtUnitSymbol.Text = DT.Rows[0][0].ToString();
            Bind_Control(true);
        }
        else
        {
            TxtQuantity.Text = "";
            TxtUnitSymbol.Text = "";
        }
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (DDLItem.SelectedValue != "0")
        {
            try
            {
                int active = 0;
                if (ChkActive.Checked == true)
                {
                    active = 1;
                }
                int EditTransaction = 0;
                if (ChkActive.Checked == true)
                {
                    EditTransaction = 1;
                }
                SqlParameter[] parm ={                                 
                                  new SqlParameter("@ITMID",DDLItem.SelectedValue),
                                  new SqlParameter("@ITMQTY",TxtQuantity.Text.Trim()),
                                  new SqlParameter("@BOMREMARKS", TxtRemarks.Text.Trim()),
                                  new SqlParameter("@BOMACTIVE",ChkActive.Checked),
                                  new SqlParameter("@EDITINTRAN",ChkEditTransaction.Checked),
                                  new SqlParameter("@CName",Session["username"].ToString())
                               };
                DU.executeprocedure("usp_insert_IVR_BOM", parm);
                string sql1 = "SELECT IDENT_CURRENT ('IVR_BOM') AS Current_Identity";
                DataTable DT = DU.getdata(sql1);
                string bomid = DT.Rows[0][0].ToString();
                InsertBOM(bomid);
                Response.Redirect("BOM.aspx?msg=insert");
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
    }
    protected void InsertBOM(string MainId )
    {
        int count = Convert.ToInt32(HidControl.Value);
        for (int i = 1; i <= count; i++)
        {
            DropDownList DDL = (DropDownList)tblItem.FindControl("DDLItemName" + i.ToString());
            if (DDL.SelectedValue != "0")
            {
                TextBox TxtQty = (TextBox)tblItem.FindControl("TxtQty" + i.ToString());
                string sql = "select PRIMARY_UOMID from IVR_ITEM where ITMID='" + DDL.SelectedValue + "'";
                DataTable DT = DU.getdata(sql);
                string uomid = DT.Rows[0][0].ToString();

                SqlParameter[] parm ={                                 
                                  new SqlParameter("@BOMID",MainId),
                                  new SqlParameter("@ITMID",DDL.SelectedValue),
                                  new SqlParameter("@ITMQTY", TxtQty.Text.Trim()),
                                  new SqlParameter("@UOMID",uomid),
                                  new SqlParameter("@CName",Session["username"].ToString())
                                     };

                DU.executeprocedure("usp_insert_IVR_BOMITEM", parm);

            }
        }
     
       
    }
    protected void BtnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
}
